import static org.hamcrest.CoreMatchers.nullValue;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.junit.Test;

import com.situ.study.entity.User;


public class JDBCTest {

	public static void main(String[] args) {
		// Alt + Shift + A
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		// 1、加载驱动Class.forName("");
		try {
			Class.forName("com.mysql.jdbc.Driver");
			// 2、获得连接对象Connection
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=UTF-8", "root", "1234");
			// 3、写sql语句
			String sql = "SELECT id,name,age,gender FROM user";
			// 4、创建Statement
			preparedStatement = connection.prepareStatement(sql);
			// 5、执行sql语句
			//      (1) 更新类（更改了表里面数据）：delete/update/insert     executeUpdate()
			//            返回值：int，表示你影响的行数
			//       (2)查询（没有改变表里面数据）:  select                  executeQuery()
			//            返回值：结果集ResultSet
			resultSet = preparedStatement.executeQuery();
			ArrayList<User> list = new ArrayList<>();
			while (resultSet.next()) {
				// 每遍历一次封装一个User对象
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				User user = new User(id, name, age, gender);
				list.add(user);
			}
			for (User user : list) {
				System.out.println(user);
			}
			// 6、关闭连接
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@Test
	public void testSelect() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT id,name,age,gender FROM user";
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			ArrayList<User> list = new ArrayList<>();
			while (resultSet.next()) {
				// 每遍历一次封装一个User对象
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				User user = new User(id, name, age, gender);
				list.add(user);
			}
			for (User user : list) {
				System.out.println(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
	}
	
	@Test
	public void testDelete() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "DELETE FROM `user` WHERE id=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, 8);
			int count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("删除成功");
			} else {
				System.out.println("删除失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
		
	}
	
	@Test
	public void testInsert() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {	
			connection = JDBCUtil.getConnection();
			String sql = "INSERT INTO user(name,age,gender) VALUES(?,?,?)";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, "张三222");
			preparedStatement.setInt(2, 23);
			preparedStatement.setString(3, "男");
			int count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("插入成功");
			} else {
				System.out.println("插入失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
		
	}
	
	@Test
	public void testUpdate() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {	
			connection = JDBCUtil.getConnection();
			String sql = "UPDATE `user` SET name=?,age=? WHERE id=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, "王五111");
			preparedStatement.setInt(2, 24);
			preparedStatement.setInt(3, 11);
			int count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("更新成功");
			} else {
				System.out.println("更新失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
		
	}
	
	
	
}
